﻿/**
* Path.js
* 
* @author Demis Bellot
* @version 1.0
* @requres Class.js, StringExt.js
*
* Copyright 2009, Demis Bellot
* http://code.google.com/p/ajaxstack/
*/

//#{name}/{arg1/arg2}?{op1=1&op2=2}
function Path(name, args, options)
{
	this.name = name;
	this.options = options || {};
	this.args = args || [];
	if (!is.Array(this.args)) this.args = [this.args];
}
Path.extend(ASObject, { type: "AjaxStack.Path" }, {
	toUrl: function()
	{
		var str = this.name;
		if (this.args.length > 0)
		{
			str += '/' + this.args.join('/');
		}
		var qs = "";
		for (var key in this.options)
		{
			qs += qs ? '&' : '?';
			qs += key + "=" + encodeURIComponent(this.options[key]);
		}
		return str + qs;
	},
	
	//IE has problems overrideing toString(), use toUrl() instead
	toString: function()
	{
		return this.toUrl();
	},

	getName: function()
	{
		return this.name;
	},

	getArgs: function()
	{
		return this.args;
	},

	getFirstArg: function()
	{
		return this.args.length > 0 ? this.args[0] : null;
	},

	setFirstArg: function(id)
	{
		return this.args[0] = id;
	},

	getOptions: function()
	{
		return this.options;
	},

	toHref: function()
	{
		return '#' + this.toUrl();
	},

	equals: function(other)
	{
		if (!other) return false;
		var otherHref = other.toHref ? other.toHref() : other.toString()
		return this.toHref() == otherHref;
	},

	nameEquals: function(otherPath)
	{
		if (!otherPath) return false;
		otherPath = Path.getPath(otherPath);
		return this.getName() == otherPath.getName();
	}
});

Path.getFirstArg = function(href)
{
	var path = Path.parse(href);
	return path ? path.getFirstArg() : null;
}
Path.getPath = function(path)
{
	if (is.TypeOf(path, Path.getType())) return path;
	return Path.parse(path);
}
Path.parse = function(href)
{
	if (!href) return null;
	if (href.indexOf('#') == -1) href = '#' + href;
	return Path.parseHref(href);
}
Path.parseHref = function(href)
{
	var href = href || window.location.href;
	var nameStartPos = href.indexOf('#');
	if (nameStartPos > -1)
	{
		var pathInfo = href.toString().substr(nameStartPos + 1);

		var options = {};
		var qsPos = pathInfo.indexOf("?");
		if (qsPos != -1)
		{
			var qs = pathInfo.substr(qsPos);
			pathInfo = pathInfo.substring(0, qsPos);
			options = Path.parseQueryString(qs);
		}

		var parts = pathInfo.split("/");
		var args = [];
		for (var i = 1; i < parts.length; i++)
		{
			args.push(unescape(parts[i]));
		}
		var path = new Path(parts[0], args, options);
		return path;
	}
	return null;
}
Path.getHostName = function(href)
{
	href = S.eat(href || window.location.href, "://");
	return href.substring(0, href.indexOf('/'));
}
Path.parseQueryString = function(href)
{
	var qString = S.eat(href || window.location.href, '?');
	if (!qString) return {};
	var qMap = {};
	var kvps = qString.split('&');
	for (var i = 0; i < kvps.length; i++)
	{
		var kvp = kvps[i].split('=');
		qMap[kvp[0]] = kvp.length == 1 ? true : kvp[1];
	}
	return qMap;
}
Path.combine = function() {
    var paths = "";
    for (var i = 0, len = arguments.length; i < arguments.length; i++) {
        
        if (paths.length > 0)
            paths += "/";

        paths += S.rtrim(arguments[i], '/');
    }
    return paths;
}